1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.util.concurrent;
18  
19  import java.util.concurrent.atomic.AtomicReference;
20  import java.util.concurrent.atomic.AtomicReferenceArray;
21  
22  import javax.annotation.Nullable;
23  
24  /**
25   * Static utility methods pertaining to classes in the
26   * {@code java.util.concurrent.atomic} package.
27   *
28   * @author Kurt Alfred Kluever
29   * @since 10.0
30   */
31  public final class Atomics {
32    private Atomics() {}
33  
34    /**
35     * Creates an {@code AtomicReference} instance with no initial value.
36     *
37     * @return a new {@code AtomicReference} with no initial value
38     */
39    public static <V> AtomicReference<V> newReference() {
40      return new AtomicReference<V>();
41    }
42  
43    /**
44     * Creates an {@code AtomicReference} instance with the given initial value.
45     *
46     * @param initialValue the initial value
47     * @return a new {@code AtomicReference} with the given initial value
48     */
49    public static <V> AtomicReference<V> newReference(@Nullable V initialValue) {
50      return new AtomicReference<V>(initialValue);
51    }
52  
53    /**
54     * Creates an {@code AtomicReferenceArray} instance of given length.
55     *
56     * @param length the length of the array
57     * @return a new {@code AtomicReferenceArray} with the given length
58     */
59    public static <E> AtomicReferenceArray<E> newReferenceArray(int length) {
60      return new AtomicReferenceArray<E>(length);
61    }
62  
63    /**
64     * Creates an {@code AtomicReferenceArray} instance with the same length as,
65     * and all elements copied from, the given array.
66     *
67     * @param array the array to copy elements from
68     * @return a new {@code AtomicReferenceArray} copied from the given array
69     */
70    public static <E> AtomicReferenceArray<E> newReferenceArray(E[] array) {
71      return new AtomicReferenceArray<E>(array);
72    }
73  }